package com.openspring.platform.controller;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.openspring.framework.common.Paging;
import com.openspring.framework.controller.GenericController;
import com.openspring.framework.util.JacksonUtils;
import com.openspring.framework.util.ResultUtils;
import com.openspring.platform.model.Dict;
import com.openspring.platform.model.Dictionary;
import com.openspring.platform.service.DictService;
import com.openspring.platform.service.DictionaryService;

@Controller
@RequestMapping("/dict")
public class DictController extends GenericController<Dict, Long> {

	private DictService dictService;
	private DictionaryService dictionaryService;

	@Resource(name="dictService")
	public void setDictService(DictService dictService) {
		this.dictService = dictService;
		super.setGenericService(dictService);
	}

	@Resource(name="dictionaryService")
	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	@RequestMapping(value="/create", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(@ModelAttribute Dict entity,
			@RequestParam(value="creates", required=false) String creates) {
		List<Dictionary> dictionaries = JacksonUtils.readValue(creates, new TypeReference<List<Dictionary>>() {});
		dictService.create(entity, dictionaries);
		return ResultUtils.datas(entity, dictionaryService.findByDictId(entity.getId()));
	}

	@RequestMapping(value="/modify", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modify(@ModelAttribute Dict entity,
			@RequestParam(value="removes", required=false) String removes,
			@RequestParam(value="modifies", required=false) String modifies,
			@RequestParam(value="creates", required=false) String creates) {
		Long[] dels = JacksonUtils.readValue(removes, new TypeReference<Long[]>() {});
		List<Dictionary> upds = JacksonUtils.readValue(modifies, new TypeReference<List<Dictionary>>() {});
		List<Dictionary> ists = JacksonUtils.readValue(creates, new TypeReference<List<Dictionary>>() {});
		dictService.modify(entity, dels, upds, ists);
		return ResultUtils.datas(entity, dictionaryService.findByDictId(entity.getId()));
	}
	
	@RequestMapping(value="/remove", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> remove(@RequestParam("id") Long[] id) {
		if (id.length == 1) {
			dictService.remove(id[0]);
		} else {
			dictService.remove(id);
		}
		return ResultUtils.success();
	}
	
	@RequestMapping(value="/load/{id}", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> load(@PathVariable("id") Long id) {
		return ResultUtils.datas(dictService.load(id), dictionaryService.findByDictId(id));
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(@ModelAttribute Paging paging,
			@RequestParam(value="filter", required=false) String filter,
			@RequestParam(value="sort", required=false) String sort) {

		return this.doQuery(paging, filter, sort);
	}

	@RequestMapping(value="/query", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> query(@ModelAttribute Paging paging,
			@RequestParam(value="filter", required=false) String filter,
			@RequestParam(value="sort", required=false) String sort) {

		return this.doQuery(paging, filter, sort);
	}
}
